﻿using System;
using System.Collections.Generic;
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using MyHotel_DTO;
using MyHotel_Common;

namespace MyHotel_DAO
{
    public class PhongDAO : DAO
    {
        protected override object createObjectFromDatarow(DataRow row)
        {
            Phong phong = new Phong();
            phong.MaPhong = Convert.ToInt32(row["Maphong"]);
            phong.TenPhong = row["TenPhong"].ToString();
            phong.LoaiPhong = Int32.Parse(row["LoaiPhong"].ToString());
            phong.TienPhong = Convert.ToInt32(row["TienPhong"]);
            phong.GhiChu = row["GhiChu"].ToString();
            phong.TinhTrang = row["TinhTrang"].ToString();
            return phong;
        }

        public List<Phong> getListPhong()
        {
            string nameProc = "USP_LAY_DANHSACH_PHONG";
            DataTable dt = DataProvider.executeStoreProcedureQuery(nameProc, null);
            return getListFromDataTable<Phong>(dt);
        }

        public List<Phong> getListPhongTrong()
        {
            string nameProc = "USP_LAY_DANHSACH_PHONGTRONG";
            DataTable dt = DataProvider.executeStoreProcedureQuery(nameProc, null);
            return getListFromDataTable<Phong>(dt);
        }

        public List<Phong> getDSPhongDangThue()
        {  
            string namestore = "usp_Danhsachphongdangthue";
            SqlParameter para = new SqlParameter();
            DataTable dt = DataProvider.executeStoreProcedureQuery(namestore, null);
            List<Phong> listPhong = new List<Phong>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Phong phong = (Phong)createObjectFromDatarow(dt.Rows[i]);
                listPhong.Add(phong);
            } 
            return listPhong;
        } 
        public List<Phong> getDSPhongConTrong()
        { 
            string namestore = "usp_Danhsachphongcontrong"; 
            DataTable dt = DataProvider.executeStoreProcedureQuery(namestore, null); 
            List<Phong> listPhong = new List<Phong>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Phong phong = (Phong)createObjectFromDatarow(dt.Rows[i]);
                listPhong.Add(phong);
            }
            return listPhong;
        }

        public List<Phong> getListPhongbyMaLoai(int maloai)
        {
            string nameProc = "SP_GETDSPHONG_BY_MALOAI";
            SqlParameter para = new SqlParameter("@maloai", maloai);
            DataTable dt = DataProvider.executeStoreProcedureQuery(nameProc, para);
            return getListFromDataTable<Phong>(dt);
        }

        public bool themPhong(Phong phong)
        {
            string nameProc = "SP_THEMPHONG";
            SqlParameter[] para = new SqlParameter[3];
            para[0] = new SqlParameter("@tenphong", phong.TenPhong);
            para[1] = new SqlParameter("@maloai", phong.LoaiPhong);
            para[2] = new SqlParameter("@ghichu", phong.GhiChu);
            
            int n = DataProvider.executeStoreProcedureNonQuery(nameProc, para);
            return (n == 1);
        }
        public bool CheckPhongByTenPhong(string tenphong)
        {
            string nameProc = "SP_GETDSPHONG_BY_TENPHONG";
            SqlParameter para = new SqlParameter("@TenPhong", tenphong);
            DataTable dt = DataProvider.executeStoreProcedureQuery(nameProc, para);
            return (dt.Rows.Count == 1);
        } 
        
        public bool capNhatPhong(Phong phong)
        {
            string nameProc = "USP_CAP_NHAT_PHONG";
            SqlParameter[] para = new SqlParameter[4];
            para[0] = new SqlParameter("@TENPHONG", phong.TenPhong);
            para[1] = new SqlParameter("@MALOAI", phong.LoaiPhong);
            para[2] = new SqlParameter("@GHICHU", phong.GhiChu);
            para[3] = new SqlParameter("@MAPHG", phong.MaPhong);

            int n = DataProvider.executeStoreProcedureNonQuery(nameProc, para);
            return (n == 1);  
        }
        public bool xoaPhong(Phong phong)
        {
            string nameProc = "USP_XOAPHONG";
            SqlParameter[] para = new SqlParameter[1];
            para[0] = new SqlParameter("@maphg", phong.MaPhong);
            int n = DataProvider.executeStoreProcedureNonQuery(nameProc, para);
            return (n == 1); 
        }

        public bool CheckPhongByTenPhong_MaPhong(string tenphong, int maphong)
        {
            string nameProc = "USP_GETPHONG_BY_TENPHONG_MAPHONG";
            SqlParameter[] para = new SqlParameter [2];
            para[0] = new SqlParameter("@tenphong", tenphong);
            para[1] = new SqlParameter("@maphg", maphong);
            DataTable dt = DataProvider.executeStoreProcedureQuery(nameProc, para); 
            return (dt.Rows.Count == 1);
        }  
    }
}
